
<?php include __DIR__ ."../../PHP/db.php"; ?>
<!-- APPLICANTS TABLE -->
<?php
  $total_pages = getTotalApplicantPages();
  $order_by = "";
  if($_GET['by'])
  {
     switch($_GET['by'])
     {
        case 'fname';
          $order_by = " ORDER BY ap.fname ASC";
        break;
        case 'lname';
          $order_by = " ORDER BY ap.lname ASC";
        break;
        case 'uname';
          $order_by = " ORDER BY a.user ASC";
        break;
     }
  }
  $limit = 3;
  $page = $_GET['appPage'];
  if($page)
  {
    $start = ($page - 1) * $limit;
  }
  else
  {
    $start = 0; 
  }

  $applicants = listApplicantAccounts($start,$limit,$order_by);

  if($page == 0) $page = 1;
  $prev = $page - 1;
  $next = $page + 1;
  $last_page = ceil($total_pages/$limit);
?>

<!-- EMPLOYERS TABLE -->
<?php
  $total_pages_e = getTotalEmployersPages();
  $limit_e = 3;
  $page_e = $_GET['empPage'];
  if($page_e)
  {
    $start_e = ($page_e - 1) * $limit_e;
  }
  else
  {
    $start_e = 0; 
  }

  $employers = listEmployersAccount($start_e,$limit_e);

  if($page_e == 0) $page_e = 1;
  $prev_e = $page_e - 1;
  $next_e = $page_e + 1;
  $last_page_e = ceil($total_pages_e/$limit_e);

?>


<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><![endif]-->
  <title>FLSS ADMIN</title>
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width">        
  <link rel="stylesheet" href="css/admin.css">

</head>
<body>
  <div id="main-wrapper">
    <div class="navbar navbar-inverse" role="navigation">
      <div class="navbar-header">
        <div class="logo"><h1>(ADMINISTRATOR)</h1></div>
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button> 
      </div>   
    </div>
    <div class="template-page-wrapper">
      <div class="navbar-collapse collapse templatemo-sidebar">
        <ul class="templatemo-sidebar-menu">
          <li>
            <form class="navbar-form">
              <input type="text" class="form-control" id="templatemo_search_box" placeholder="Search...">
              <span class="btn btn-default">Go</span>
            </form>
          </li>
          <li><a href="adminHome.php"><i class="fa fa-home"></i>Home</a></li>
          <li class="sub">
            <a href="javascript:;">
              <i class="fa fa-database"></i> Notifications <div class="pull-right"><span class="caret"></span></div>
            </a>
            <ul class="templatemo-submenu">
              <li><a href="AppReg.php">Applicant Registration</a></li>
              <li><a href="EmpReg.php">Employer posts Registration</a></li>
              <li><a href="EmpIntSched.php">Employer Interview Schedules</a></li>             
              <li><a href="EmpSignup.php">Employer Sign Up Approval</a></li>
              <li><a href="HiredApp.php">Hired Applicants</a></li>
            </ul>
          </li>
          <li class="active"><a href="#"><i class="fa fa-users"></i>Manage Users</a></li>
          <li><a href="archives.php"><i class="fa fa-book"></i>Archives</a></li>
          <li><a href="javascript:;" data-toggle="modal" data-target="#confirmModal"><i class="fa fa-sign-out"></i>Sign Out</a></li>
        </ul>
      </div><!--/.navbar-collapse -->

      <div class="templatemo-content-wrapper">
        <div class="templatemo-content">
          <ol class="breadcrumb">
            <li><a href="adminHome.php">Admin Panel</a></li>
            <li><a href="#">Manage Users</a></li>
            <li class="active">Tables</li>
          </ol>
          <h1>Manage Users</h1>
          <p>Here goes tables and users.</p>

         
          <div class="row">
            <div class="col-md-12">
              <div class="btn-group pull-right" id="templatemo_sort_btn">
                <button type="button" class="btn btn-default">Sort by</button>
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                  <span class="caret"></span>
                  <span class="sr-only">Toggle Dropdown</span>
                </button>
                <ul class="dropdown-menu" role="menu">

                  <li><a href="<?php if($_GET['appPage']){ echo 'tables.php?appPage='.$_GET["appPage"].'&by=fname';}else{ echo 'tables.php?appPage=1&by=fname'; }?>">First Name</a></li>
                  <li><a href="<?php if($_GET['appPage']){ echo 'tables.php?appPage='.$_GET["appPage"].'&by=lname';}else{ echo 'tables.php?appPage=1&by=lname'; }?>">Last Name</a></li>
                  <li><a href="<?php if($_GET['appPage']){ echo 'tables.php?appPage='.$_GET["appPage"].'&by=uname';}else{ echo 'tables.php?appPage=1&by=uname'; }?>">Username</a></li>
                </ul>
              </div>
              <div class="table-responsive">
                <h4 class="margin-bottom-15">Applicants Table</h4>
                <table class="table table-striped table-hover table-bordered">
                  <thead>
                    <tr>
                      <th>#</th>
                      <th>First Name</th>
                      <th>Last Name</th>
                      <th>Username</th>
                      <th>Email</th>
                      
                      <th>Delete</th>
                    </tr> 
                  </thead>
                  <tbody>
                  <?php while($row = mysql_fetch_array($applicants)){ ?>
                    <tr id="a_id_<?=$row['applicant_id']?>">
                      <td><?=$row['applicant_id']?></td>
                      <td><?=$row['fname']?></td>
                      <td><?=$row['lname']?></td>
                      <td><?=$row['user']?></td>
                      <td><?=$row['email']?></td>                
                     
                      <td><a href="#" a_id="<?=$row['applicant_id']?>" class="btn btn-link btnDeleteApp">Delete</a></td>
                    </tr>
                  <?php } ?>
                              
                  </tbody>
                </table>
              </div>
                
                <ul class="pagination pull-right">
                <li class="disabled"><a href="tables.php?appPage=<?=$prev?>">&laquo;</a></li>
                <?php for($current = 1;$current <= $last_page;$current ++) { ?>
                <li class="<?php if($current == $page) echo 'active'; ?>"><a href="tables.php?appPage=<?=$current?>"><?=$current?><span class="sr-only">(current)</span></a></li>
                <?php } ?>
                <li><a href="tables.php?appPage=<?=$next?>">&raquo;</a></li>
                </ul>
                <br><br><br>
                  
              <div class="table-responsive">
                <h4 class="margin-bottom-15">Employers Table</h4>
                <table class="table table-striped table-hover table-bordered">
                  <thead>
                    <tr>
                      <th>#</th>
                      <th>Company Name</th>
                      <th>Email</th>
                      <th>Username</th>
                      
                      <th>Delete</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php while($row = mysql_fetch_array($employers)) { ?>
                    <tr id="e_id_<?=$row['employer_id']?>">
                      <td><?=$row['employer_id']?></td>
                      <td><?=$row['ename']?></td>
                      <td><?=$row['user']?></td>
                      <td><?=$row['email']?></td>                 
                      
                      <td><a href="#" e_id="<?=$row['employer_id']?>" class="btn btn-link btnDeleteEmp">Delete</a></td>
                    </tr>                    
                    <?php }?>
                  </tbody>
                </table>
              </div>
              <ul class="pagination pull-right">
                <li class="disabled"><a href="#">&laquo;</a></li>
                <?php for($current = 1;$current <= $last_page_e;$current ++) { ?>
                <li class="<?php if($current == $page_e) echo 'active'; ?>"><a href="tables.php?empPage=<?=$current?>"><?=$current?><span class="sr-only">(current)</span></a></li>
                <?php } ?>
                <li><a href="#">&raquo;</a></li>
              </ul>  
            </div>
          </div>
        </div>
      </div>

      <!-- Modal -->
      <div class="modal fade" id="confirmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
              <h4 class="modal-title" id="myModalLabel">Are you sure you want to sign out?</h4>
            </div>
            <div class="modal-footer">
              <a href="sign-in.html" class="btn btn-primary">Yes</a>
              <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
            </div>
          </div>
        </div>
      </div>

      <footer class="templatemo-footer">
        <div class="templatemo-copyright">
          <p>Copyright &copy; 2084 Your Company Name <!-- Credit: www.templatemo.com --></p>
        </div>
      </footer>
    </div>
</div>
    <script src="js/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/admin.js"></script>

    <script type="text/javascript">

    function deleteApplicantAccount(app_id)
    {
      var params = {};
      params.app_id = app_id;
       return $.ajax({
          url: '../api/delete_applicant_account.php',
          data: params,
          type: 'post',
          dataType: 'json'
       });  
    }

    function deleteEmployerAccount(emp_id)
    {
      var params = {};
      params.emp_id = emp_id;
       return $.ajax({
          url: '../api/delete_employer_account.php',
          data: params,
          type: 'post',
          dataType: 'json'
       });  
    }
      
      $('.btnDeleteApp').click(function(){
            var app_id = $(this).attr('a_id');
           
            var r = confirm("Are you sure you want to delete this account?");
            if(r== true)
            {
               var loading = "Processing  <img src= '../images/loading.gif'/>";
               var cur = $(this);
               cur.parent().html(loading);

                deleteApplicantAccount(app_id).done(function(result){
                    if(result.status == "true")
                    {
                         $('#a_id_'+app_id).fadeOut(2000);             
                    }
                });    
            }      
        });
        
        $('.btnDeleteEmp').click(function(){
            var emp_id = $(this).attr('e_id');
            var r = confirm("Are you sure you want to delete this account?");
 
            if(r== true)
            {
               var loading = "Processing  <img src= '../images/loading.gif'/>";
               var cur = $(this);
               cur.parent().html(loading);

                deleteEmployerAccount(emp_id).done(function(result){
                    if(result.status == "true")
                    {
                        $('#e_id_'+emp_id).fadeOut(2000);            
                    }

                });    
            }
        });
    </script>
  </body>
</html>